package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyParkTicketRenewAddBo;
import com.ruoyi.system.bo.ZsyyParkTicketRenewQueryBo;
import com.ruoyi.system.bo.ZsyyParkTicketRenewEditBo;
import com.ruoyi.system.domain.ZsyyParkTicketRenew;
import com.ruoyi.system.mapper.ZsyyParkTicketRenewMapper;
import com.ruoyi.system.vo.ZsyyParkTicketRenewVo;
import com.ruoyi.system.service.IZsyyParkTicketRenewService;
import com.ruoyi.system.mapper.ZsyyParkTicketRenewMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 月票续费记录Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkTicketRenewServiceImpl extends ServicePlusImpl<ZsyyParkTicketRenewMapper, ZsyyParkTicketRenew> implements IZsyyParkTicketRenewService {
    @Autowired
    private ZsyyParkTicketRenewMapper iZsyyParkTicketRenewMapper;

    @Override
    public ZsyyParkTicketRenewVo queryById(Long id){
        return getVoById(id, ZsyyParkTicketRenewVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkTicketRenewVo> queryPageList(ZsyyParkTicketRenewQueryBo bo) {
        PagePlus<ZsyyParkTicketRenew, ZsyyParkTicketRenewVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyParkTicketRenewVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyParkTicketRenewVo> queryList(ZsyyParkTicketRenewQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyParkTicketRenewVo.class);
    }

    private LambdaQueryWrapper<ZsyyParkTicketRenew> buildQueryWrapper(ZsyyParkTicketRenewQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyParkTicketRenew> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMonthTicketBillId() != null, ZsyyParkTicketRenew::getMonthTicketBillId, bo.getMonthTicketBillId());
        lqw.eq(StrUtil.isNotBlank(bo.getBillCode()), ZsyyParkTicketRenew::getBillCode, bo.getBillCode());
        lqw.eq(StrUtil.isNotBlank(bo.getParkCodes()), ZsyyParkTicketRenew::getParkCodes, bo.getParkCodes());
        lqw.eq(bo.getMonthTicketId() != null, ZsyyParkTicketRenew::getMonthTicketId, bo.getMonthTicketId());
        lqw.eq(bo.getOriginPrice() != null, ZsyyParkTicketRenew::getOriginPrice, bo.getOriginPrice());
        lqw.eq(bo.getFavorPrice() != null, ZsyyParkTicketRenew::getFavorPrice, bo.getFavorPrice());
        lqw.eq(bo.getPayOrigin() != null, ZsyyParkTicketRenew::getPayOrigin, bo.getPayOrigin());
        lqw.eq(StrUtil.isNotBlank(bo.getPayOriginRemark()), ZsyyParkTicketRenew::getPayOriginRemark, bo.getPayOriginRemark());
        lqw.eq(bo.getPayMode() != null, ZsyyParkTicketRenew::getPayMode, bo.getPayMode());
        lqw.eq(StrUtil.isNotBlank(bo.getPayModeRemark()), ZsyyParkTicketRenew::getPayModeRemark, bo.getPayModeRemark());
        lqw.eq(StrUtil.isNotBlank(bo.getRenewBy()), ZsyyParkTicketRenew::getRenewBy, bo.getRenewBy());
        lqw.eq(bo.getRenewTime() != null, ZsyyParkTicketRenew::getRenewTime, bo.getRenewTime());
        lqw.eq(bo.getStartTime() != null, ZsyyParkTicketRenew::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, ZsyyParkTicketRenew::getEndTime, bo.getEndTime());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkTicketRenewAddBo bo) {
        ZsyyParkTicketRenew add = BeanUtil.toBean(bo, ZsyyParkTicketRenew.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkTicketRenewEditBo bo) {
        ZsyyParkTicketRenew update = BeanUtil.toBean(bo, ZsyyParkTicketRenew.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyParkTicketRenew entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
